﻿using System;
using System.Collections.Generic;
using System.Text;
using LLFM.CAT.Config;
using Common;
using System.Data;
using System.Data.SqlClient;
using LLFM.CAT.Model;

namespace LLFM.CAT.Service
{
    public sealed class SeriesService
    {
        /// <summary>
        /// 插入产品系列
        /// </summary>
        /// <param name="name"></param>
        /// <param name="weight"></param>
        /// <param name="advantage"></param>
        /// <param name="shortDescription"></param>
        /// <param name="description"></param>
        /// <param name="CoverImageDetail"></param>
        /// <param name="coverImage"></param>
        /// <param name="coverImageId"></param>
        /// <param name="status"></param>
        public static int CreateSeries(string name
            , string weight
            , string advantage
            , string shortDescription
            , string description
            , string CoverImageDetail
            , string coverImage
            , int coverImageId
            , int status
            , DateTime lastUpdateTime)
        {
            object obj = SqlHelper.ExecuteScalar(DBConfig.BitautoSDT
                , CommandType.StoredProcedure
                , "pro_CreateSeries"
                , new SqlParameter("@Name", name)
                , new SqlParameter("@Weight", weight)
                , new SqlParameter("@Advantage", advantage)
                , new SqlParameter("@ShortDescription", shortDescription)
                , new SqlParameter("@Description", description)
                , new SqlParameter("@CreateTime", DateTime.Now)
                , new SqlParameter("@CoverImageDetail", CoverImageDetail)
                , new SqlParameter("@CoverImage", coverImage)
                , new SqlParameter("@CoverImageId", coverImageId)
                , new SqlParameter("@Status", status)
                , new SqlParameter("@LastUpdateTime", lastUpdateTime));
            return Common.TypeParse.StrToInt(obj, 0);
        }

        /// <summary>
        /// 得到所有系列
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public static List<M_Series> GetSeriesList(int status)
        {
            DataTable dt = SqlHelper.ExecuteDataset(DBConfig.BitautoSDT
                , CommandType.StoredProcedure
                , "pro_GetSeriesList"
                , new SqlParameter("@Status", status)).Tables[0];
            return ModelHelper.FillModelsFromDataTable<M_Series>(dt);
        }

        /// <summary>
        /// 根据Id得到系列
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static M_Series GetSeriesById(int id) 
        {
            DataTable dt = SqlHelper.ExecuteDataset(DBConfig.BitautoSDT
               , CommandType.StoredProcedure
               , "pro_GetSeriesById"
               , new SqlParameter("@Id", id)).Tables[0];
            return ModelHelper.FillModel<M_Series>(dt);
        }

        /// <summary>
        /// 修改系列
        /// </summary>
        /// <param name="id"></param>
        /// <param name="name"></param>
        /// <param name="weight"></param>
        /// <param name="advantage"></param>
        /// <param name="shortDescription"></param>
        /// <param name="description"></param>
        /// <param name="createTime"></param>
        /// <param name="CoverImageDetail"></param>
        /// <param name="coverImage"></param>
        /// <param name="coverImageId"></param>
        /// <param name="status"></param>
        /// <param name="lastUpdateTime"></param>
        /// <returns></returns>
        public static int UpdateSeries(int id
            , string name
            , string weight
            , string advantage
            , string shortDescription
            , string description
            , string CoverImageDetail
            , string coverImage
            , int coverImageId
            , int status
            , DateTime lastUpdateTime)
        {
            return SqlHelper.ExecuteNonQuery(DBConfig.BitautoSDT
                , CommandType.StoredProcedure
                , "pro_UpdateSeries"
                , new SqlParameter("@Id", id)
                , new SqlParameter("@Name", name)
                , new SqlParameter("@Weight", weight)
                , new SqlParameter("@Advantage", advantage)
                , new SqlParameter("@ShortDescription", shortDescription)
                , new SqlParameter("@Description", description)
                , new SqlParameter("@CoverImageDetail", CoverImageDetail)
                , new SqlParameter("@CoverImage", coverImage)
                , new SqlParameter("@CoverImageId", coverImageId)
                , new SqlParameter("@Status", status)
                , new SqlParameter("@LastUpdateTime", lastUpdateTime));
        }

        public static List<M_Series> GetSeriesListForPage(int status, int pageIndex, int pageSize, out int count, out int pageCount)
        {
            SqlParameter[] parms = {
                                       new SqlParameter("@Status",status)
                                       ,new SqlParameter("@PageIndex",pageIndex)
                                       ,new SqlParameter("@PageSize",pageSize)
                                     ,new SqlParameter("@RecordCount",SqlDbType.Int, 4, ParameterDirection.Output, false, 0, 0, string.Empty, DataRowVersion.Default, null),
                                     new SqlParameter("@PageCount",SqlDbType.Int, 4, ParameterDirection.Output, false, 0, 0, string.Empty, DataRowVersion.Default, null)
                                   };
            DataTable dt = SqlHelper.ExecuteDataset(DBConfig.BitautoSDT, CommandType.StoredProcedure, "pro_GetSeriesListForPage"
                , parms).Tables[0];
            count = Convert.ToInt32(parms[3].Value);
            pageCount = Convert.ToInt32(parms[4].Value);
            return ModelHelper.FillModelsFromDataTable<M_Series>(dt);
        }


    }
}
